package com.wmlive.hhvideo.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by XueFei on 2017/6/7.
 * <p>
 * 处理图片
 */

public class ImageUtil {
    /**
     * 将图片放到文件中
     * @param filePath
     * @param bitmap
     * @return
     */
    public static boolean setBitmapToFile(String filePath, Bitmap bitmap) {
        boolean result = false;
        try {
            File file = new File(filePath);
            FileOutputStream fos = new FileOutputStream(file);
            // CompressFormat压缩
            if (filePath != null
                    && (filePath.contains(".png") || filePath
                    .contains(".PNG"))) {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            } else {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            }
            fos.flush();
            if (fos != null) {
                fos.close();
            }
            result = true;
            return result;
        } catch (Exception e) {
            System.err.println("setBitmapToFile image file fail");
            result = false;
            return result;
        }
    }

    /**
     * 获取本地图片
     * @param filePath
     * @return
     */
    public static Bitmap getBitmapFromFile(String filePath) {
        Bitmap bitmap = null;
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inSampleSize = 1;

        if (filePath != null) {
            try {
                File file = new File(filePath);
                FileInputStream fis = new FileInputStream(file);
                bitmap = BitmapFactory.decodeStream(fis, null, opt);
            } catch (Exception e) {
                bitmap = null;
            }
        }
        return bitmap;
    }

    /**
     * 将图片转成byte[]
     * @param bmp
     * @param needRecycle
     * @return
     */
    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();//自动销毁
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 旋转图片
     * @param path
     * @return
     */
    private int readBitmapDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * 旋转图片
     * @param angle
     * @param bitmap
     * @return
     */
    private Bitmap rotateBitmap(int angle, Bitmap bitmap) {
        // 旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        return resizedBitmap;
    }
}
